"""
Copyright (c) 2024 Beijing Jiaotong University
PhotLab is licensed under [Open Source License].
You can use this software according to the terms and conditions of the [Open Source License].
You may obtain a copy of [Open Source License] at: [https://open.source.license/]

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.

See the [Open Source License] for more details.

Author: Yunzhou Tang
Created: 2024/02/14
Supported by: National Key Research and Development Program of China
"""

import phot
import json
import matplotlib.pyplot as plt

""" 导入测试用例数据 """
json_file_path = "./test/test_system_B2B.json" 
with open(json_file_path, 'r',encoding='utf-8') as file:
    data_ = json.load(file) 
data=data_['1']

if __name__ == "__main__":
    """ 本代码为程序主函数 本代码主要适用于 QPSK, 8QAM, 16QAM, 32QAM, 64QAM 调制格式的单载波相干背靠背(B2B)信号 """

    phot.config(plot=True, backend="torch")  # 配置基本信息 开启画图 使用torch作为运算库后端

    """ 设置全局系统仿真参数 """
    # 符号数目
    num_symbols = data['global_param']['num_symbols']['value']
    # 2 for QPSK; 4 for 16QAM; 6 for 64QAM  设置调制格式                           
    bits_per_symbol = data['global_param']['bits_per_symbol']['value']       
    # 信号波特率，符号率                       
    total_baud = data['global_param']['total_baud']['value']   
    # 上采样倍数                        
    up_sampling_factor = data['global_param']['up_sampling_factor']['value']       
    # 信号采样率                   
    sampling_rate = up_sampling_factor * total_baud  
    Reference_frequency = data['global_param']['Reference_frequency']['value']

    """ 发射端 """
    """ RRC """
    RRC_ROLL_OFF = data['tx_param']['RRC_ROLL_OFF']['value']

    """ DAC """
    DAC_Resolution_Bits = data['tx_param']['DAC_Resolution_Bits']['value']
    DAC_OUTPUT_VPP = data['tx_param']['DAC_OUTPUT_VPP']['value']

    """ 光源 """
    Linewidth = data['tx_param']['Linewidth']['value']
    output_power = data['tx_param']['output_power']['value']

    """ 调制器 """
    Extinction_ratio = data['tx_param']['Extinction_ratio']['value']
    VPI = data['tx_param']['VPI']['value']
    VDC = data['tx_param']['VDC']['value']

    """ OSNR """
    osnr = data['tx_param']['osnr']['value']

    signals = phot.transmitter(num_symbols, bits_per_symbol, total_baud, up_sampling_factor, Reference_frequency,
                               RRC_ROLL_OFF, DAC_Resolution_Bits, DAC_OUTPUT_VPP, Linewidth, output_power, Extinction_ratio, VPI, VDC, osnr)

    """ 接收端 """
    """ 光源 """
    LO_Linewidth = data['rx_param']['LO_Linewidth']['value']
    frequency_offset = data['rx_param']['frequency_offset']['value']
    LO_power = data['rx_param']['LO_power']['value']

    """" 相干接收机 """
    Responsivity = data['rx_param']['Responsivity']['value']

    """ ADC """
    ADC_sampling_rate = data['rx_param']['ADC_sampling_rate']['value']
    ADC_resolution_bits = data['rx_param']['ADC_resolution_bits']['value']

    """ RRC """
    RRC_ROLL_OFF = data['rx_param']['RRC_ROLL_OFF']['value']

    """ 自适应均衡器 """
    num_tap = data['rx_param']['num_tap']['value']
    ref_power_cma = data['rx_param']['ref_power_cma']['value']
    cma_convergence = data['rx_param']['cma_convergence']['value']
    step_size_cma = data['rx_param']['step_size_cma']['value']
    step_size_rde = data['rx_param']['step_size_rde']['value']

    """ BPS """
    num_test_angle = data['rx_param']['num_test_angle']['value']
    block_size = data['rx_param']['block_size']['value']

    signals = phot.receiver(signals, num_symbols, bits_per_symbol, total_baud, up_sampling_factor, Reference_frequency, LO_Linewidth, frequency_offset, LO_power,
                            Responsivity, ADC_sampling_rate, ADC_resolution_bits, RRC_ROLL_OFF, num_tap, ref_power_cma, cma_convergence, step_size_cma, step_size_rde, num_test_angle, block_size)

    # 分析器画星座图
    # phot.constellation_diagram(signals)
    signals , plot_num = phot.constellation_diagram(signals,isdata = True)
    print(f'plot_num {plot_num}')
    print(f'signals {signals}')
    print(f'len of signals {len(signals)}')

    axis_x = signals[0][0]
    axis_y = signals[1][0]
    plt.figure(figsize=(10, 10))
    plt.scatter(axis_y, axis_x, s=1)
    plt.xlabel('xlabel')
    plt.ylabel('ylabel')
    plt.title('title')
    plt.grid()
    plt.show()

    """ 此处开始计算误码率 """
    # 返回误码率和 Q 影响因子
    # ber, q_factor = phot.bits_error_count(
    #     signals, bits_per_symbol)

    # print(ber)
    # print(q_factor)
